<template>
  <div>
    <div class="toolbar">
      <el-button type="primary">返回</el-button>
      <el-button type="primary" @click="createProvince()">新增</el-button>
    </div>
    <el-table :data="citys" v-loading="loading"
              highlight-current-row>
      <el-table-column prop="name" label="城市名称" width="500" align="center"></el-table-column>
      <el-table-column prop="code" label="城市代码" align="center"></el-table-column>
      </el-table-column>
      <el-table-column label="操作" width="300" inline-template align="center">
        <template>
          <el-button @click="modifyCity(row)" type="text" size="small">查看详情</el-button>
          <el-button @click="getAreas(row.pkid)" type="text" size="small">区县信息</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="按钮权限" v-model="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="城市名称" required prop="name">
          <el-input v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="城市代码" required prop="code">
          <el-input v-model="form.code" :disabled="form.pkid" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="省份" required prop="provinceId">
          <el-select v-model="form.provinceId" :disabled="provinceId" placeholder="请选择省份">
            <el-option
              v-for="item in provinces"
              :label="item.name"
              :value="item.pkid">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style scoped>
  .page{
    text-align: right;
    margin: 10px 0px;
  }
</style>
<style>
  .toolbar{
    padding: 10px;
  }
</style>
<script>
  import cityApi from '../../../api/sys/cityApi'
  import provinceApi from '../../../api/sys/provinceApi'

  export default {
    data () {
      return {
        citys: [],
        loading: false,
        provinceId: '',
        dialogFormVisible: false,
        provinces: [],
        form: {pkid: null, name: '', code: '', provinceId: ''},
        rules: {
          name: [
            { required: true, message: '请输入城市名称', trigger: 'blur' }
          ],
          right: [
            { required: true, message: '请输入城市代码', trigger: 'blur' }
          ]
        }
      }
    },
    created () {
      this.loading = true
      provinceApi.getProvinces().then((resp) => {
        this.provinces = resp.body.data
      })
      this.provinceId = this.$route.params.provinceid
      cityApi.getCitys(this.provinceId).then((resp) => {
        this.citys = resp.body.data
        this.loading = false
      })
    },
    methods: {
      createCity () {
        this.dialogFormVisible = true
        this.form = {pkid: null, name: '', code: '', provinceId: this.provinceId}
      },
      modifyCity (city) {
        this.dialogFormVisible = true
        this.form = city
      },
      save () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            cityApi.saveCity(this.form).then((resp) => {
              if (!this.form.pkid) {
                this.form.pkid = resp.body.data.pkid
                this.citys.push(this.form)
              }
              this.dialogFormVisible = false
              this.$message({
                showClose: true,
                message: '保存成功',
                type: 'success'
              })
            })
          } else {
            return false
          }
        })
      },
      getAreas (id) {
        this.$router.push({name: 'cityAreas', params: {cityid: id, provinceid: this.$route.params.provinceid}})
      }
    }
  }
</script>
